<?php
/*Problem Domain
To create a class that represents the List of User defined types
*/

/*
- ListUserType is new ListUserType(String , array)

- interp. new ListUserType(list_name, array_list) represents the ListUserType where
	- list_name is name given to that list of Sessions to attend by trainee
	- array_list is the actal array of Session class

- Example.
$los1 = new ListUserType('week1_sessions' , array($sess1 , $sess2));
$lost = new ListUserType('week2_sessions');

- Template
function fn-for-ListUserType()
{
	...$this->list_name;	//String
	...$this->array_list;
}
*/
class ListUserType
{
	private $list_name;
	private $array_list;

	/*String, array 
	purpose. initializes the newly created List Of Sessions*/
	public function __construct($list_name = null, $array_list = null)
	{
		$this->list_name = $list_name;
		$this->array_list 	= $array_list; 
	}
	
	/*void -> String
	purpose. produces the List name of the current ListUserType object*/
	public function getListName()
	{
		return $this->list_name;
	}

	/*String -> void
        purpose. sets the given List name for the current ListUserType object*/
        public function setListName($list_name)
        {
                $this->list_name = $list_name;
        }

	/*void -> ListUserType
        purpose. produces the List Of Sessions of the current User*/
        public function getArrayList()
        {
                return $this->array_list;
        }

        /*ListUserType -> void
        purpose. sets the given List Of Sessions for the current User*/
        public function setArrayList(ListUserType $array_list)
        {
                $this->array_list = $array_list;
        }

	/*Session -> boolean
	purpose. adds the given Session to the current ListUserType and returns true on success , false otherwise.*/
	public function addToList($sess)
	{
		$status = true;

		if(!($this->array_list[] = $sess))
		{
			$status = false;
		} 

		return $status;
	}

	/*Session -> boolean
	purpose. produces true(position) of the given User type in the current List of User Type,
	false(-1) otherwise*/
	private function getPosition($obj)
	{
		$pos = -1;

		for($i = 0 ; $i < count($this->array_list); $i++)
		{
			if($this->array_list[$i]->getId() == $obj->getId())
			{
				$pos = $i;
				break;
			}
		}

		return $pos;
	}

	/*Session -> boolean
        purpose. removes the given Session from the current ListUserType and returns true on success , false otherwise.*/
        public function removeFromList($obj)
	{
		$status = true;

		if(($pos = $this->getPosition($obj)) != -1)
		{
			if(!array_splice($this->array_list , $pos , 1))
			{
				$status = false;
			}
		}
		else
		{
			echo 'the given session not found';
			$status = false;
		}

		return $status;
	}

}
?>
